package com.lhb.email.service.impl;





import com.lhb.email.service.EmailService;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * @author 86131
 * @version 1.0
 * @description 邮箱服务实现类
 * @date 2025/3/30 下午3:46
 */
@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String senderEmail;
    @Override
    public void sendVerificationEmail(String receiverEmail, String code) throws MessagingException, UnsupportedEncodingException, jakarta.mail.MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(senderEmail,"博易云团队");
        helper.setTo(receiverEmail);
        helper.setSubject("博易云");
        String htmlContent = "<html><body>"
                + "<h3 style='color:blue'>您的验证码：<strong>" + code + "</strong></h3>"
                + "<p>有效期：5分钟</p>"
                + "<p style='color:red'>请勿将此验证码告知他人！</p>"
                + "</body></html>";
        helper.setText(htmlContent,true);
        mailSender.send(message);
    }

    @Override
    public void sendNewPasswordEmail(String receiverEmail, String newPassword) throws MessagingException, UnsupportedEncodingException, jakarta.mail.MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(senderEmail,"博易云团队");
        helper.setTo(receiverEmail);
        helper.setSubject("博易云");
        String htmlContent = "<html><body>"
                + "<h3 style='color:blue'>您的新密码：<strong>" + newPassword + "</strong></h3>"
                + "<p style='color:red'>请勿将此密码告知他人！</p>"
                + "</body></html>";
        helper.setText(htmlContent,true);
        mailSender.send(message);
    }

    @Override
    public void sendRegisterInfoToEmail(String email) throws MessagingException, UnsupportedEncodingException, jakarta.mail.MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(senderEmail,"博易云团队");
        helper.setTo(email);
        helper.setSubject("博易云");
        String htmlContent = "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>欢迎加入博易云</title>\n" +
                "    <style>\n" +
                "        * {\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "            box-sizing: border-box;\n" +
                "            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;\n" +
                "        }\n" +
                "        \n" +
                "        body {\n" +
                "            background-color: #f8fafc;\n" +
                "            padding: 20px;\n" +
                "            display: flex;\n" +
                "            justify-content: center;\n" +
                "            min-height: 100vh;\n" +
                "        }\n" +
                "        \n" +
                "        .container {\n" +
                "            max-width: 500px;\n" +
                "            width: 100%;\n" +
                "            background: white;\n" +
                "            border-radius: 8px;\n" +
                "            overflow: hidden;\n" +
                "            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);\n" +
                "        }\n" +
                "        \n" +
                "        .header {\n" +
                "            background: #1e5799;\n" +
                "            padding: 30px 20px;\n" +
                "            text-align: center;\n" +
                "            color: white;\n" +
                "        }\n" +
                "        \n" +
                "        .header h1 {\n" +
                "            font-size: 24px;\n" +
                "            font-weight: 600;\n" +
                "            margin-bottom: 10px;\n" +
                "        }\n" +
                "        \n" +
                "        .header p {\n" +
                "            font-size: 14px;\n" +
                "            opacity: 0.9;\n" +
                "        }\n" +
                "        \n" +
                "        .content {\n" +
                "            padding: 30px;\n" +
                "        }\n" +
                "        \n" +
                "        .welcome-text {\n" +
                "            font-size: 16px;\n" +
                "            line-height: 1.7;\n" +
                "            color: #444;\n" +
                "            margin-bottom: 20px;\n" +
                "        }\n" +
                "       \n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <div class=\"header\">\n" +
                "            <h1>欢迎加入博易云</h1>\n" +
                "            <p>感谢您注册我们的平台</p>\n" +
                "        </div>\n" +
                "        \n" +
                "        <div class=\"content\">\n" +
                "            <p class=\"welcome-text\">尊敬的客户，您好！</p>\n" +
                "            <p class=\"welcome-text\">感谢您注册博易云服务，您的账户已成功创建。</p>    \n" +
                "        </div>\n" +
                "        \n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
        helper.setText(htmlContent,true);
        mailSender.send(message);
    }
}
